package android.medicalcloud.com.yiapp.yilian.ui;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.yilian.ui.view.NoScrollViewPager;
import android.medicalcloud.com.yiapp.yilian.ui.view.adapter.MainPagerAdapter;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class MainPageActivity extends BaseActivity {
    TabLayout tabLayout;
    NoScrollViewPager viewPager;
    TextView titleText;
    CheckBox homecheckbox;
    CheckBox ordercheckbox;
    CheckBox datecheckbox;
    CheckBox personcheckbox;
    MainPagerAdapter adapter;
    String[] titles = new String[]{"首页", "订单", "日程", "我的"};
    View mainToolbar;

    @Override
    protected void initView() {
        tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        viewPager = (NoScrollViewPager)findViewById(R.id.viewPager);
        titleText = (TextView)findViewById(R.id.titleText);
        homecheckbox = (CheckBox)findViewById(R.id.homecheckbox);
        ordercheckbox = (CheckBox)findViewById(R.id.ordercheckbox);
        datecheckbox = (CheckBox)findViewById(R.id.datecheckbox);
        personcheckbox = (CheckBox)findViewById(R.id.personcheckbox);
        mainToolbar = findViewById(R.id.mainToolbar);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        viewPager.setOffscreenPageLimit(10);
        adapter = new MainPagerAdapter(getSupportFragmentManager(), this);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);



        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                titleText.setText(titles[arg0]);

                setDefault();
                switch (arg0) {
                    case 0:
                        homecheckbox.setChecked(true);
                        break;
                    case 1:
                        ordercheckbox.setChecked(true);
                        mainToolbar.setVisibility(View.GONE);
                        break;
                    case 2:
                        datecheckbox.setChecked(true);
                        break;
                    case 3:
                        personcheckbox.setChecked(true);
                        mainToolbar.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        setDefault();
        homecheckbox.setChecked(true);
        homecheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
                setDefault();
                homecheckbox.setChecked(true);
            }
        });
        ordercheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);

                setDefault();
                ordercheckbox.setChecked(true);
                mainToolbar.setVisibility(View.GONE);
            }
        });
        datecheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
                setDefault();
                datecheckbox.setChecked(true);
                mainToolbar.setVisibility(View.GONE);
            }
        });
        personcheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
                setDefault();
                personcheckbox.setChecked(true);
                mainToolbar.setVisibility(View.GONE);
            }
        });

    }

    private void setDefault() {
        homecheckbox.setChecked(false);
        ordercheckbox.setChecked(false);
        datecheckbox.setChecked(false);
        personcheckbox.setChecked(false);
        mainToolbar.setVisibility(View.VISIBLE);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.ac_main;
    }
}
